/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.suli.tasks.repository;

import me.zhengjie.suli.tasks.domain.Tasks;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
 * @author Allen Gao
 * @website https://eladmin.vip
 * @date 2022-12-30
 **/
public interface TasksRepository extends JpaRepository<Tasks, String>, JpaSpecificationExecutor<Tasks> {

    @Query(value = "select * from tasks where task_code = ?1 ", nativeQuery = true)
    public Tasks getTaskByTaskCode(String taskCode);


    @Query(value = "select * from tasks where traycode = ?1 AND location_from NOT IN ('CY01', 'CY02' ) order by create_time desc ", nativeQuery = true)
    List<Tasks> findByTrayCode(String trayCode);


    @Query(value = "select * from tasks where task_code = ?1  order by create_time desc ", nativeQuery = true)
    Tasks findByTaskCode(String taskCode);

    @Query(value = "select * from tasks where task_code = ?1 order by create_time desc ", nativeQuery = true)
    List<Tasks> findByTaskCodeList(String taskCode);

    @Query(value = "select * from tasks where task_code in (?1) order by create_time desc ", nativeQuery = true)
    List<Tasks> findByTaskCodes(List<String> taskCode);

    @Query(value = "select * from tasks where source_code = ?1", nativeQuery = true)
    List<Tasks> findBySourceCode(String sourceCode);

    @Query(value = "select top(1)* from tasks where task_type = 10 AND status != 2 AND traycode = ?1 ORDER BY start_time desc", nativeQuery = true)
    Tasks findByTypeAndTrayCodeAndStatus(String trayCode);



    @Query(value = "select top(1) * from tasks where source_code = ?1", nativeQuery = true)
    Tasks findBySourceCodeTop(String sourceCode);

    @Query(value = "select * from tasks where source_code = ?1 AND trayCode in (?2)", nativeQuery = true)
    List<Tasks> findBySourceCodeAAndTrayCodes(String sourceCode, List<String> trayCode);

    @Query(value = "select * from tasks where source_code in (?1)", nativeQuery = true)
    List<Tasks> findBySourceCodes(List<String> sourceCode);

    @Query(value = "select * from tasks where task_code = ?1 order by create_time desc ", nativeQuery = true)
    Tasks findByTaskCodeAndUu(String taskCode);

    /** 数据大图 */
    @Query(value = "select top(20) * from tasks where status = 1 AND (task_type = 1 or task_type = 2) order by create_time desc", nativeQuery = true)
    List<Tasks> getTop20TasksWithStatus1();

    /** 根据载具和状态查找盘点出库任务 */
    @Query(value = "select top(1) * from tasks where traycode = ?1 and [status] = ?2 and task_type = 5 order by create_time desc ", nativeQuery = true)
    Tasks getCheckOutTaskByTrayCodeAndStatus(String trayCode, int status);
}